#include "GameState.h"
//#include "astar.h"

using namespace Ogre;

void GameState::changeState( GameState *state ) { 
    GameManager::getSingletonPtr()->changeState( state ); 
}

void GameState::pushState( GameState *state ) { 
    GameManager::getSingletonPtr()->pushState( state ); 
}

void GameState::popState( void ) { 
    GameManager::getSingletonPtr()->popState(); 
}

void GameState::requestShutdown( void ) {
    GameManager::getSingletonPtr()->requestShutdown();
}

void GameState::playSFX( const char *_fileName ){
	if( mSoundEngine ) {
		mSoundEngine->play2D( _fileName, false, false, false, ESM_AUTO_DETECT, false );
	}
}

void GameState::playSFX3D( const char *_fileName, irrklang::vec3df position){
	if( mSoundEngine ) {
		//mSoundEngine->play2D( _fileName, false, false, false, ESM_AUTO_DETECT, false );
		mSoundEngine->play3D(_fileName, position, false, false, false, ESM_AUTO_DETECT, false );
	}
}

void GameState::playMusic( const char *_fileName, bool bln ){
	if( mSoundEngine ){
		if( mSoundEngine->isCurrentlyPlaying( _fileName )) {
			this->stopMusic();
		}
		this->mMusic = mSoundEngine->play2D( _fileName, bln, false, true, ESM_STREAMING, false );
		mMusic->setVolume(0.8);
	}
}

void GameState::pauseMusic( ){
	if( mMusic )
		mMusic->setIsPaused( true );
}

void GameState::resumeMusic( ){
	if( mMusic && mMusic->getIsPaused() )
		mMusic->setIsPaused( false );
}

void GameState::stopMusic( ){
	if( mMusic ){
		mMusic->stop();
		mMusic->drop();
		mMusic = 0;
	}
}
/*
void increaseMusicVolume(ik_f32 _step){
		mMusic->setVolume(mMusic->getVolume()+_step);
}
void decreaseMusicVolume(ik_f32 _step){
		mMusic->setVolume(mMusic->getVolume()-_step);
}*/

void GameState::setAudioVolume( irrklang::ik_f32 _volume ){
	if( _volume < 0.0f )
		mSoundEngine->setSoundVolume( 0.0f );
	else if( _volume > 1.0f )
		mSoundEngine->setSoundVolume( 1.0f);
	else
		mSoundEngine->setSoundVolume( _volume );
}

irrklang::ik_f32 GameState::getAudioVolume(){
	return mSoundEngine->getSoundVolume();
}

void GameState::unloadAllSounds(){
	
	// unload used soudSources if any
	if( mSoundEngine )	{
		irrklang::ik_s32 max = mSoundEngine->getSoundSourceCount();
		for( irrklang::ik_s32 i= max-1; i>=0 && i>mInitialSources-1 ; i-- )
			mSoundEngine->removeSoundSource( mSoundEngine->getSoundSource( i ) );
	}
	
	stopMusic();
}

void GameState::animate( unsigned long lTimeSinceLastFrame ) {
	
	/*
	std::vector<Character*>::iterator itc;
	itc = characters.begin();
	while( itc != characters.end()) {
		(*itc)->animate( lTimeSinceLastFrame );		
		itc++;
	}

	std::vector<WorldItem*>::iterator itw;
	itw = worlditems.begin();
	while( itw != worlditems.end()) {
		(*itw)->animate( lTimeSinceLastFrame );		
		itw++;
	}

	std::vector<UseableItem*>::iterator itu;
	itu = useableitems.begin();
	while( itu != useableitems.end()) {
		(*itu)->animate( lTimeSinceLastFrame );		
		itu++;
	}

	std::vector<InventoryItem*>::iterator iti;
	iti = inventoryitems.begin();
	while( iti != inventoryitems.end()) {
		(*iti)->animate( lTimeSinceLastFrame );		
		iti++;
	}
	*/
}